package cn.tedu.charging.device.dao.repository.impl;

import cn.tedu.charging.device.dao.repository.AbstractStationCacheRepository;
import cn.tedu.charging.device.dao.repository.StationCacheRepository;
import cn.tedu.charging.device.pojo.po.StationCanalPO;
import cn.tedu.charging.device.pojo.po.StationPO;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.*;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.core.GeoOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Repository("GeoAndString")
public class StationCacheGeoAndStringRepositoryImpl
        extends AbstractStationCacheRepository
        implements StationCacheRepository {

    @Autowired
    private RedisTemplate redisTemplate;

    private static final String STATIONS_KEY = "stations_mapping";
    private static final String STATION_INFO_STR_KEY = "station_info_string_";

    @Override
    public void saveAllStations(List<StationPO> stations) {
        saveLocationByGEOAndMemberIsId(stations);
        saveStationInfo2String(stations);
    }


    private void saveStationInfo2String(List<StationPO> stations) {
        //场站id和场站PO的映射
        Map<String,StationPO> idPOMap = new HashMap<>();
        stations.forEach(stationPO -> {
            idPOMap.put(STATION_INFO_STR_KEY + stationPO.getId(),stationPO);
        });
        ValueOperations valueOperations = redisTemplate.opsForValue();
        valueOperations.multiSet(idPOMap);
    }

    @Override
    public List<StationPO> search(Double lng, Double lat, Integer distance) {
        return super.search(lng, lat, distance);
    }

    @Override
    public void insert(StationCanalPO stationCanalPO) {
        //todo
    }

    @Override
    public void update(StationCanalPO before, StationCanalPO after) {
        //todo
    }

    @Override
    public void delete(StationCanalPO stationCanalPO) {
        //todo
    }

    @Override
    public StationPO getStationById(Integer stationId) {
        ValueOperations<String,StationPO> valueOperations =
                redisTemplate.opsForValue();
        return valueOperations.get(stationId);
    }

    @Override
    public RedisTemplate getRedisTemplate() {
        return redisTemplate;
    }
}
